package com.ddpt.permission.util;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.lh.center.common.util.LoggerUtil;
import org.apache.commons.lang3.StringUtils;

/**
 * 发送短信验证码工具
 */
public class PhoneCodeUtil {

    private static String zhuanXinUrl = "https://api.zhuanxinyun.com/api/v2/sendSms.json?appKey=n2KV7CFqs7yzzEE9rRemeBj0RyBEmKTs&appSecret=91780ca3183acd6a7e96bd1274e8b8d2&phones=#{phone}&content=【联动营销云】您的登录验证码:#{code}";

    /**
     * 发送短信验证码
     * @param phone 手机号码
     */
    public static boolean send(String phone,String code){
        if(StringUtils.isBlank(phone)){
            return false;
        }
        if(!PhoneUtil.isPhone(phone)){
            return false;
        }
        String res = HttpUtil.get(zhuanXinUrl.replace("#{phone}", phone).replace("#{code}", code));
        boolean send = "000000".equals(JSONUtil.parseObj(res).getStr("errorCode"));
        if(!send){
            LoggerUtil.info(PhoneCodeUtil.class,"用户登录验证码发送-->手机号:{},验证码:{},响应:{}",phone,code,res);
        }
        return send;
    }

    public static boolean send(String phone){
        return send(phone, RandomUtil.randomString("0123456789",4));
    }
}
